<?php

namespace backend\controllers;

use backend\models\JjrUserCommission;
use Yii;
use backend\models\JjrUserWithdraw;
use yii\data\ActiveDataProvider;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * JjrUserWithdrawController implements the CRUD actions for JjrUserWithdraw model.
 */
class JjrUserWithdrawController extends BaseController
{
    public $enableCsrfValidation = false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all JjrUserWithdraw models.
     * @return mixed
     */
    public function actionIndex()
    {
        $query = JjrUserWithdraw::find()
            ->alias('uw')
            ->select("uw.*,p.name,u.real_name,up.client_real_name")
            ->leftJoin('yii2_jjr_user_commission uc','uw.jjr_user_commission_id=uc.id')
        ->leftJoin('yii2_jjr_user_project up','uc.jjr_user_project_id=up.id')
        ->leftJoin('yii2_jjr_project p','up.jjr_project_id=p.id')
        ->leftJoin('yii2_jjr_user u','up.jjr_user_id=u.id')->asArray();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single JjrUserWithdraw model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new JjrUserWithdraw model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new JjrUserWithdraw();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing JjrUserWithdraw model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $userCommission = JjrUserCommission::findOne(['id'=>$model->jjr_user_commission_id]);
            if($model->transfer_status== JjrUserWithdraw::TRANSFER_END){
                 $userCommission->updateAttributes(['transfer_status'=>JjrUserCommission::TRANSFER_END]);
            }else if($model->transfer_status== JjrUserWithdraw::TRANSFER_FAIL){
                $userCommission->updateAttributes(['transfer_status'=>JjrUserCommission::TRANSFER_NOT_START]);
            }else if($model->transfer_status== JjrUserWithdraw::TRANSFER_STARTED){
                $userCommission->updateAttributes(['transfer_status'=>JjrUserCommission::TRANSFER_STARTED]);
            }
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing JjrUserWithdraw model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the JjrUserWithdraw model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return JjrUserWithdraw the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = JjrUserWithdraw::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
